package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.Basic.util.AjaxResult;
import cn.itsource.pethome.Basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门controller类
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 增加部门
     * @param department //部门
     * @return
     */
    //@RequestMapping(value = "/save",method = RequestMethod.PUT)
    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"增加失败"+e.getMessage());
        }
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping(value = "{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }
    /**
     * 修改部门
     * @param department
     * @return
     */
    //@RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Department department){
        try {
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    //@RequestMapping(value = "/findById",method = RequestMethod.GET)
    @GetMapping("{id}")
    public Department findById(@PathVariable("id") Long id){
          return departmentService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //@RequestMapping(value = "/findAll",method = RequestMethod.PATCH)
    @PatchMapping
    public PageList<Department> findByQuery(@RequestBody DepartmentQuery departmentQuery){
          return departmentService.findByQuery(departmentQuery);
    }
    @PatchMapping("/findAll")
    public List<Department> findAll(){
          return departmentService.findAll();
    }
    @PatchMapping(value = "/deleteAll")
    public AjaxResult delete(@RequestBody Long[] ids){
        try {
            departmentService.deleteIds(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }
}
